<!--
 * @Descripttion: 
 * @version: v1.O.0
 * @Author: 薛玉峰
 * @Date: 2020-02-16 10:04:42
 * @LastEditors  : 薛玉峰
 * @LastEditTime : 2020-02-16 20:53:38
 -->
<!DOCTYPE html>
<html>
	<head>
		<%-include("./public/head.ejs")%>
	</head>
	<body>
		<form
			class="layui-form"
			action="/admin/white_ip/edit"
			method="post"
			style="margin-right: 35px;"
		>
			<input type="hidden" name="id" value="<%= id %>" />
			<div class="layui-form-item" style="margin-top: 30px;">
				<label class="layui-form-label">IP</label>
				<div class="layui-input-inline">
					<input
						type="text"
						name="ip"
						lay-verify="ip"
						autocomplete="off"
						class="layui-input"
						value="<%= ip %>"
					/>
				</div>
			</div>

			<div class="layui-form-item">
				<div class="layui-input-block">
					<button
						class="layui-btn"
						lay-submit=""
						lay-filter="profile"
					>
						保存
					</button>
				</div>
			</div>
		</form>
		<script src="/layui/layui.js" charset="utf-8"></script>
		<script type="text/javascript">
			layui.use(["layer", "form"], function() {
				var layer = layui.layer,
					$ = layui.jquery,
					form = layui.form;
				//自定义验证规则
				form.verify({
					ip: function(value) {
						let re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;

						if (!re.test(value)) {
							return "IP 格式不正确！";
						}
						// 验证是否是内网
						let lan_network = ["127.", "172.", "10.", "192."];
						let _isLan = false;
						for (let i in lan_network) {
							if (0 == value.indexOf(lan_network[i])) {
								_isLan = true;
								break;
							}
						}
						if (true === _isLan) return "不能为内网ip";
					}
				});

				form.on("submit(profile)", function(data) {
					function success(data) {
						if (0 == data.code) {
							layer.msg(data.msg);
							setTimeout(function() {
								var index = parent.layer.getFrameIndex(
									window.name
								); //获取窗口索引
								parent.layer.close(index);
							}, 1000);
						} else {
							layer.msg(data.msg, {
								icon: 2,
								time: 1000
							});
						}
					}
					$.post(data.form.action, data.field, success, "json");
					return false;
				});
			});
		</script>
	</body>
</html>
